﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmMarkWorkDetail : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int _EmployeeID = 0;
        int _BranchID = 0;
        int _DepartmentID = 0;
        bool _loadedData = false;
        DateTime _fromdate = DateTime.MinValue;
        MarkWorkClient _wcfMarkwork;

        decimal _TongSoCong = 0;
        public frmMarkWorkDetail()
        {
            InitializeComponent();
            BuildVoucherGrid();

            _wcfMarkwork = Service.MarkWork();

            UISetting.LoadTreeView3(ref tvMarkWork);
            dtpdate.Value = DateTime.Today;

        }
        void LoadLanguage()
        {
            if (this.Tag != null)
            {
                this.TabText = this.Text = UpdateUI.GetString(this.Tag.ToString());
                UpdateUI.Load_ToolStrip(ref toolStrip1);
            }
        }

        TreeNode GetBranchNode(TreeNode treeNode)
        {
            TreeNode RetVal = treeNode;
            if (RetVal.Level >= 2)
            {
                RetVal = treeNode.Parent;
            }
            return RetVal;
        }

        private void tvMarkWork_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                _BranchID = 0;
                _DepartmentID = 0;
                _EmployeeID = 0;
            }
            if (e.Node.Level == 1)
            {
                _BranchID = Convert.ToInt32(e.Node.Tag);
                _DepartmentID = 0;
            }
            if (e.Node.Level >= 2)
            {
                _BranchID = Convert.ToInt32(GetBranchNode(e.Node).Tag);
                _DepartmentID = Convert.ToInt32(e.Node.Tag);
            }
            UISetting.LoadEmployeeByDepartment(ref cboEmployee, _DepartmentID);

        }
        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "MarkWork_CheckDate", "Thứ", "", 100, true, true);
            UISetting.AddColumn(ref dgvList, "MarkWork_Date", "Ngày", "C", 100, true, true);
            UISetting.AddColumn(ref dgvList, "MarkWork_TimeIn", "Giờ vào", "C", 80, true, true);
            UISetting.AddColumn(ref dgvList, "MarkWork_TimeOut", "Giờ ra", "C", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Markwork_WorkTime", "Số giờ làm", "R", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Markwork_Cong", "Số công", "R", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Shift_Name", "Ca", "", 80, true, true);
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        
        private void LoadData()
        {
            DataSet ds = _wcfMarkwork.MarkWork_GetList(out itemCount, currPage, pageSize, _EmployeeID, _fromdate);
            PageIndex();
            dgvList.Rows.Clear();
            _TongSoCong = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);

                dgvList.Rows[i].Cells["MarkWork_Date"].Value = Convert.ToDateTime(ds.Tables[0].Rows[i]["MarkWork_Date"]).ToString("dd/MM/yyyy");
                dgvList.Rows[i].Cells["MarkWork_TimeIn"].Value = Convert.ToDateTime(ds.Tables[0].Rows[i]["MarkWork_TimeIn"]).ToString("HH:mm");
                dgvList.Rows[i].Cells["MarkWork_TimeOut"].Value = Convert.ToDateTime(ds.Tables[0].Rows[i]["MarkWork_TimeOut"]).ToString("HH:mm");
                string _DayofWeek = "";
                switch (Convert.ToDateTime(ds.Tables[0].Rows[i]["MarkWork_CheckDate"]).DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        _DayofWeek = "Hai";
                        break;
                    case DayOfWeek.Tuesday:
                        _DayofWeek = "Ba";
                        break;
                    case DayOfWeek.Wednesday:
                        _DayofWeek = "Tư";
                        break;
                    case DayOfWeek.Thursday:
                        _DayofWeek = "Năm";
                        break;
                    case DayOfWeek.Friday:
                        _DayofWeek = "Sáu";
                        break;
                    case DayOfWeek.Saturday:
                        _DayofWeek = "Bảy";
                        break;
                    case DayOfWeek.Sunday:
                        _DayofWeek = "CN";
                        break;
                }

                dgvList.Rows[i].Cells["MarkWork_CheckDate"].Value = _DayofWeek;
                dgvList.Rows[i].Cells["Markwork_WorkTime"].Value = Convert.ToInt32(ds.Tables[0].Rows[i]["Markwork_WorkTime"]).ToString();
                dgvList.Rows[i].Cells["Markwork_Cong"].Value = Convert.ToDecimal(ds.Tables[0].Rows[i]["Markwork_Cong"]).ToString("0.00");
                dgvList.Rows[i].Cells["Shift_Name"].Value = ds.Tables[0].Rows[i]["Shift_Name"].ToString();

                _TongSoCong += Convert.ToDecimal(ds.Tables[0].Rows[i]["Markwork_Cong"]);
            }

            lbtongsocong.Text = "Tổng số công : "+_TongSoCong.ToString("0.00");
            _loadedData = true;
        }
        private void dtpdate_ValueChanged(object sender, EventArgs e)
        {
            _fromdate = dtpdate.Value;
            LoadData();
        }

        private void cboEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_loadedData)
            {
                _EmployeeID = Convert.ToInt32(cboEmployee.ComboBox.SelectedValue);
                LoadData();
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;

            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage< totalPage- 1)
                currPage++;

            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage > 0)
                currPage = totalPage- 1;

            LoadData();
        }

        private void frmMarkWorkDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            _wcfMarkwork.Close();
        }

    }
}
